<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\matrix\controller;
use cmf\controller\PluginMatrixRestBaseController;
use plugins\Demo\Model\PluginDemoModel;
use think\Db;
use think\Validate;
use tree\Tree;

/**
 * Class ApiDepartmentController
 * http://192.168.2.219/plugin/matrix/api_department/
 * @package plugins\matrix\controller
 */
class ApiDepartmentController extends PluginMatrixRestBaseController
{
    /**
     * 构造函数
     */
    public function _initialize()
    {
        if (empty($this->user)) {
            $this->error(['code' => 10000, 'msg' => '未登录']);
        }
    }

    public function index()
    {
        $result     = Db::name('matrix_department')->order(["list_order" => "ASC"])->fieldRaw("id,parent_id,name")->select();
        $tree     = new Tree();
        $array    = [];
        foreach ($result as $r) {
            $array[]       = $r;
        }
        $tree->init($array);
        return $this->success("OK",$tree->getChildrenTreeArray(0));
    }

    public function queryalllist()
    {
        $where["id"] = array('neq',1);
        $result     = Db::name('matrix_department')->where($where)->order(["list_order" => "ASC"])->fieldRaw("id,parent_id,name")->select();
        return $this->success("OK",$result);
    }



    /*public function index_api()
    {
        $result     = Db::name('matrix_department')->order(["list_order" => "ASC"])->fieldRaw("id,parent_id,name")->select()->toArray();
        $parenttree = array();
        $childtree = array();
        $i = 0;
        $index = 0;
        foreach ($result as $key => $value) {
            if ($value['parent_id'] === 0 || $value['parent_id'] == 1) {
                $info["id"] = $value["id"];
                $info["type"] = 1;
                $info["lable"] = $value["name"];
                $parenttree[$i] = $info;
                $i++;
            }else{
                $info["id"] = $value["id"];
                $info["type"] = 2;
                $info["lable"] = $value["name"];
                $info["parent_id"] = $value["parent_id"];
                $childtree[$index] = $info;
                $index++;
            }
        }

        foreach ($childtree as $child){
            $i = 0;
            foreach ($parenttree as &$parent){
                if($child["parent_id"] == $parent["id"]){
                    unset($child["parent_id"]);
                    $parent["children"][$i] = $child;
                }
            }
        }
        return $this->success("OK",$parenttree);
    }*/


    //http://192.168.2.219/plugin/matrix/api_department/addPost/parent_id/1/name/开发部.html
    public function addPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'parent_id'          => 'require',
                'name'          => 'require'
            ]);

            $validate->message([
                'parent_id.require'          => '父节点不能为空!',
                'name.require'          => '名称不能为空!'
            ]);
            $data = $this->request->param();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }else{
                $info['list_order'] = 100;
                $info["parent_id"] = $data["parent_id"];
                $info["name"] = $data["name"];
                $info['status'] = 1;
                $info['create_time'] = time();

                $result = DB::name('matrix_department')->insertGetId($info);
                if ($result !== false) {
                    $this->success("添加成功!");
                } else {
                    $this->error("添加失败！");
                }
            }
        }else{
            $this->error("参数异常！");
        }
    }


    public function editPost()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'id'          => 'require',
                'name'          => 'require'
            ]);
            $validate->message([
                'id.require'          => 'ID不能为空!',
                'name.require'          => '名称不能为空!'
            ]);
            $data = $this->request->param();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }else{
                $info["name"] = $data["name"];
                $result  = DB::name('matrix_department')->where(array("id"=>$data["id"]))->update($info);
                if ($result !== false) {
                    $this->success("修改成功!");
                } else {
                    $this->error("修改失败！");
                }
            }
        }else{
            $this->error("参数异常！");
        }
    }

    public function delete()
    {
        $validate = new Validate([
            'id'          => 'require'
        ]);
        $validate->message([
            'id.require'          => 'ID不能为空!'
        ]);
        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }else{
            if($data["id"] != 1){
                $result = DB::name('matrix_department')->where(["id" => $data["id"]])->delete();
                if ($result !== false) {
                    $this->success("删除成功!");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("顶级部门不能删除！");
            }
            
        }
    }

}
